package com.ruoyi.tmsdriveracc.service.impl;

import com.ruoyi.tmsdriveracc.domain.ADriverReceipt;
import com.ruoyi.tmsdriveracc.domain.AWaybill;
import com.ruoyi.tmsdriveracc.domain.DriverCheck;
import com.ruoyi.tmsdriveracc.mapper.ADriverReceiptDao;
import com.ruoyi.tmsdriveracc.mapper.AWaybillDao;
import com.ruoyi.tmsdriveracc.mapper.DriverCheckMapper;
import com.ruoyi.tmsdriveracc.service.ADriverReceiptService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (ADriverReceipt)表服务实现类
 *
 * @author 小何
 * @since 2022-04-24 00:20:17
 */
@Service("aDriverReceiptService")
public class ADriverReceiptServiceImpl implements ADriverReceiptService {
    @Resource
    private ADriverReceiptDao aDriverReceiptDao;
    @Resource
    private DriverCheckMapper driverCheckMapper;
    @Resource
    private AWaybillDao aWaybillDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ADriverReceipt queryById(Integer id) {
        ADriverReceipt aDriverReceipt = new ADriverReceipt();
        //查回执
        aDriverReceipt = aDriverReceiptDao.queryById(id);
        //根据合同查运单号
        DriverCheck driverCheck = driverCheckMapper.queryById(id);
        //差貨物信息
        AWaybill aWaybill = aWaybillDao.queryById(driverCheck.getWaybillId());
        aDriverReceipt.setaWaybill(aWaybill);
        return aDriverReceipt;
    }

    /**
     * 分页查询
     *
     * @param aDriverReceipt 筛选条件
     * @param pageRequest    分页对象
     * @return 查询结果
     */
    @Override
    public Page<ADriverReceipt> queryByPage(ADriverReceipt aDriverReceipt, PageRequest pageRequest) {
        long total = this.aDriverReceiptDao.count(aDriverReceipt);
        return new PageImpl<>(this.aDriverReceiptDao.queryAllByLimit(aDriverReceipt, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param aDriverReceipt 实例对象
     * @return 实例对象
     */
    @Override
    public ADriverReceipt insert(ADriverReceipt aDriverReceipt) {
        this.aDriverReceiptDao.insert(aDriverReceipt);
        return aDriverReceipt;
    }

    /**
     * 修改数据
     *
     * @param aDriverReceipt 实例对象
     * @return 实例对象
     */
    @Override
    public ADriverReceipt update(ADriverReceipt aDriverReceipt) {
        this.aDriverReceiptDao.update(aDriverReceipt);
        return this.queryById(aDriverReceipt.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.aDriverReceiptDao.deleteById(id) > 0;
    }
}
